package org.fing.edu.uy.esbadp.motor.strategies;

import java.util.List;
import java.util.logging.Level;

import org.fing.edu.uy.esbadp.core.AdpFlowCache;
import org.fing.edu.uy.esbadp.core.AdpFlowInterface;
import org.fing.edu.uy.esbadp.core.constants.EsbAdpServiceMetadataKeys;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.entity.GenericTreeNode;
import org.fing.edu.uy.esbadp.core.entity.PropertiesMonitoring;
import org.fing.edu.uy.esbadp.core.interfaces.IAdpStrategy;
import org.fing.edu.uy.esbadp.logger.LoggerADP;
import org.fing.edu.uy.esbadp.motor.comunication.AdmAdaptationComunication;

public class AdpStrategyUseStoreData implements IAdpStrategy{
	
	private static final java.util.logging.Logger logger = LoggerADP.getLogger(AdpStrategyUseStoreData.class.getSimpleName());
	
	/*
	 * FlowTree = CACHE + VirtualService
	 */
	@Override
	public GenericTreeNode<AdpFlowInterface> getAdpTree(DMRegisteredService service,List<DMRegisteredService> equivServices, PropertiesMonitoring properties) {
		try{
			//chequeo que se pueda realizar la estrategia
			if(service.containsMetadata(EsbAdpServiceMetadataKeys.dataService) && service.getMetadata(EsbAdpServiceMetadataKeys.dataService).trim().equalsIgnoreCase("true")){
				GenericTreeNode<AdpFlowInterface> root = new GenericTreeNode<AdpFlowInterface>();
				AdpFlowInterface action = new AdpFlowCache(service.getVirtualServiceName(), service.getVirtualServiceCategory());
				root.setData(action);
				GenericTreeNode<AdpFlowInterface> virtualServiceNode = AdmAdaptationComunication.getAdpNodeForInvokeVirtualService(service);
				if (virtualServiceNode != null){
					root.addChild(virtualServiceNode);		
					return root;
				}else{
					logger.log(Level.SEVERE, "Error in getAdpNodeForInvokeVirtualService");
					return null;
				}
			}else{
				return null;						
			}
		}catch(Exception ex){
			logger.log(Level.SEVERE, "Exception in AdpStrategyDelayService...\n StackTarce => [ {0} ]", ex);	
			return null;
		}
	}
	
}
